package com.ht.sps.service.impl;

import com.ht.sps.service.BaseService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.FieldFilter;
import org.nutz.dao.entity.EntityField;
import org.nutz.dao.pager.Pager;
import org.nutz.service.EntityService;
import org.nutz.trans.Atom;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 薄成文 13-10-10 上午10:46
 */
public abstract class BaseServiceImpl<T> extends EntityService<T> implements BaseService<T> {
    @Override
    public void init() {
    }

    public BaseServiceImpl() {
        super();
    }

    public BaseServiceImpl(Dao dao) {
        super(dao);
    }

    public BaseServiceImpl(Dao dao, Class<T> entityType) {
        super(dao, entityType);
    }

    @Override
    public T fetch(long id) {
        return dao().fetch(getEntityClass(), id);
    }

    @Override
    public Integer getMaxId() {
        return dao().getMaxId(getEntityClass()) & 0xFFFFFFFF;
    }

    @Override
    public boolean isExist(long id) {
        EntityField ef = getEntity().getIdField();
        if (null == ef)
            return false;
        return dao().count(getEntityClass(), Cnd.where(ef.getName(), "=", id)) > 0;
    }

    public boolean isExist(Cnd cnd) {
        EntityField ef = getEntity().getIdField();
        if (null == ef)
            return false;
        return dao().count(getEntityClass(), cnd) > 0;
    }

    @Override
    public T insert(T t) {
        return dao().insert(t);
    }

    @Override
    public void delete(long id) {
        dao().delete(getEntityClass(), id);
    }

    @Override
    public void delete(T entity) {
        dao().delete(entity);
    }

    @Override
    public void update(T t) {
        dao().update(t);
    }

    @Override
    public T findOne(Condition cnd) {
        List<T> list = query(cnd, null);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<T> findAll() {
        return query(null, null);
    }

    /**
     * 分页查询表中所有数据
     *
     * @param currentPage 当前页数
     * @param pageSize    每页显示数量
     * @param orderby     desc排序的条件
     * @return List
     */
    @Override
    public List<T> findByPage(int currentPage, int pageSize, String orderby) {
        Pager pager = dao().createPager(currentPage, pageSize);

        return query(Cnd.orderBy().desc(orderby), pager);
    }

    @Override
    public T fetchByFilter(final long id, String regex) {
        final List<T> list = new ArrayList();
        FieldFilter.create(getEntityClass(), regex).run(new Atom() {
            public void run() {
                T t = fetch(id);
                if (t != null) {
                    list.add(t);
                }
            }
        });
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<T> fetchByFilter(final Condition cnd, String regex) {
        final List<T> list = new ArrayList();
        FieldFilter.create(getEntityClass(), regex).run(new Atom() {
            public void run() {
                List<T> l = query(cnd, null);

                if (l != null) {
                    list.addAll(l);
                }
            }
        });
        return list;
    }

    @Override
    public void updateByFilter(final T t, String regex) {
        FieldFilter.create(getEntityClass(), regex).run(new Atom() {
            public void run() {
                insert(t);
            }
        });
    }

}
